<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML" 4.01"//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!-- Created: August 22, 2001-->
<!--
$Id: SurveyGenData.php,v 1.6 2001/12/19 17:17:10 eugenem Exp $
$Header: /export/home/eugenem/public_html/ComplianceReports/RCS/SurveyGenData.php,v 1.6 2001/12/19 17:17:10 eugenem Exp $
$Log: SurveyGenData.php,v $
Revision 1.6  2001/12/19 17:17:10  eugenem
Removed an extra require() statement that was messing things up.

Revision 1.5  2001/11/02 19:10:53  eugenem
Moved HTTP_*_VARS processing to dbc.inc

Revision 1.4  2001/11/02 15:00:42  eugenem
Moved database connect statements to a separate file.

Revision 1.3  2001/08/25 21:04:56  eugenem
Convert linearity, accuracy, beam quality, reproducibility values into bit flags and stored into GenData/UseFlags field.

Revision 1.2  2001/08/23 17:38:25  eugenem
Bug fixes to make everything work properly.

Revision 1.1  2001/08/22 19:13:44  eugenem
Initial revision

-->

<html>
<head>
<title>Radiological Equipment Database</title>
<meta name="author" content="Eugene Mah">
<link rev="made" href="mailto:eugenem@ix.netcom.com">
<link rev="home" href="http://home.netcom.com/~eugenem/">
<meta name="description" content="">
<meta name="Keywords" content="">
<meta name="Reply-to" content="eugenem@ix.netcom.com">
<META http-equiv="PICS-Label" content='(PICS-1.1 "http://www.classify.org/safesurf/" l r (SS~~0001))'>
<?PHP
require('functions.inc');
require('dbc.inc');

/*
  Define constants that indicate what each line is used for
*/
define("LINEARITY", 0x1);  // Low bit
define("ACCURACY", 0x2);
define("BEAMQUAL", 0x4);
define("REPRO", 0x8);  // High bit

?>
</head>

<body>
<h1>Radiological Equipment Database</h1>
<?PHP
if (isset($SurveyID)) {
  /*
    Verify that generator data from $SurveyID doesn't already exist
    in the DB
  */
  if (mysql_num_rows(mysql_query("select GenID from GenData where SurveyID=$SurveyID")) > 0) {
    echo "<h2>Data already exists</h2>\n";
    echo "<p>Generator data already exists for Survey $SurveyID.\n</p>";
    break;
  }

  /*
    We have entered this from by submitting SurveyAddRec.php accessed
    from the main menu.  A $SurveyID has been given, find the $MachID
    associated with that survey.
  */
  $row = mysql_fetch_array(mysql_query("select Machines.MachineID,Description from Machines,TestDates
       where Machines.MachineID=TestDates.MachineID and SurveyID=$SurveyID"));
  $MachID = $row['MachineID'];
  $desc = $row['Description'];

  /*
    Decide which tube belongs with the unit corresponding to the
    SurveyID
  */
  $query = "select TubeID,Description from Machines,Tubes where ";
  $query .= "Machines.MachineID=Tubes.MachineID and Machines.MachineID=$MachID ";
  $query .= "and Machines.Active=1 and Tubes.Active=1";

  $result = mysql_query($query)
    or die ("<p>Unable to execute query: $query</p>");;
  $n = mysql_num_rows($result);  // How many rows did we get back

  /*
    Convert the contents of $GenData containing the pasted generator data
    into an array.
  */
  $gdrows = explode("\n",$GenData);
  $n = count($gdrows);
  $useflag = 0;
  for ($i=0;$i<$n-1;$i++) {
    $query = "INSERT INTO GenData (SurveyID,kVset,mAset,Timeset,";
    $query .= "mAsset,AddFilt,Distance,UseFlags,kVAvg,kVMax,kVEff,ExpTime,Exp) ";
    $query .= "values ($SurveyID,$MachID,";
    /*
      Split each row into an array with each element
      containing a column of data after rtrim()ming any trailing spaces
      or newlines.
    */
    $gdcol = explode("\t",rtrim($gdrows[$i]));
    /* Only columns 1-4,6,8,17-19,21,24-28 are used. */
    $query .= "$gdcol[0],$gdcol[1],$gdcol[2],$gdcol[3],$gdcol[5],$gdcol[7],";

    /*
      Take the linearity, accuracy, beam quality and reproducibility flags
      from the table and pack it all into one byte
      bit 0 - linearity
      bit 1 - accuracy
      bit 2 - beam quality
      bit 3 - reproducibility
    */
    $useflag = ($gdcol[16] << 0) | ($gdcol[17] << 1) | ($gdcol[18] << 2) | ($gdcol[20] << 3);

    $query .= "$useflag,";

    /*
      If there is no value, then replace it with a 0
    */
    for ($j=23;$j<=26;$j++) {
      if (empty($gdcol[$j])) { $query .= "0,"; }
      else { $query .= "$gdcol[$j],"; }
    }

    /*
      The last array element has a newline tacked onto it.
      Use trim() to remove it, and tack on a trailing 0 in case
      $gdcol[27] is empty after trim()ming..
    */
    if (empty($gdcol[27])) { $query .= "0)"; }
    else { $query .= "$gdcol[27])"; }

    $result = mysql_query($query)
      or die("<p>Unable to execute query: $query</p>");
    //echo "<p>$query</p>\n";
  }
  echo "<h2>Generator Data Added</h2>\n";
  echo "<p>Generator data added for Survey $SurveyID</p>\n";
}
?>
<h2>Add generator data</h2>
<form action="SurveyGenData.php" method="POST">
<p>Select a survey: 
<?PHP
$survID = mysql_query("select SurveyID from TestDates");
echo listbox_one_field($survID,"SurveyID","");
?>
</p>
<!--
Data pasted from the spreadsheet generator data block should consist of
28 columns x 39 rows of tab delimited values.  Only columns 1-4,6,8,
17-19,21,24-28 are used.
-->
<p><textarea name="GenData" rows=39 cols=200>Paste selection from spreadsheet</textarea></p>
<p><input type="SUBMIT" value="Add data"> / <input type="RESET" value="Reset"> / <a href="index.html" target="_top" >Main</a></p>
</form>
<?PHP mysql_close(); ?>
</body>
</html>
